package com.heer.gymnasium.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.Interface.OnHeightChangeListener;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.MyPagerAdapter;
import com.heer.gymnasium.adapter.OrderPagerAdapter;
import com.heer.gymnasium.adapter.PlaceCateTitleAdapter;
import com.heer.gymnasium.adapter.SiteDetailsRechargeAdapter;
import com.heer.gymnasium.constant.Extras;
import com.heer.gymnasium.fragment.BaseFragment;
import com.heer.gymnasium.fragment.SiteDetailsTypeFragment;
import com.heer.gymnasium.util.LoadImageUtil;
import com.heer.gymnasium.util.SystemUtils;
import com.heer.gymnasium.widget.MyListView;
import com.heer.model.AdSiteBean;
import com.heer.model.SiteOfferBean;
import com.heer.model.SitePlaceCateBean;
import com.heer.model.SitesBean;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by heer on 17/4/6.
 */

public class SiteDetailsActivity extends BaseActivity implements OnHeightChangeListener {
    @BindView(R.id.layout_root)
    ScrollView scrollView;
    @BindView(R.id.viewpager_scene)
    AutoScrollViewPager viewpagerScene;
    @BindView(R.id.indicator_scene)
    MagicIndicator indicatorScene;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.rating_level)
    RatingBar ratingLevel;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.btn_call)
    ImageView btnCall;
    @BindView(R.id.image_recharge_arrow)
    ImageView imageRechargeArrow;
    @BindView(R.id.list_recharge)
    ListView listRecharge;
    @BindView(R.id.btn_recharge_more)
    TextView btnRechargeMore;

    @BindView(R.id.layout_recharge)
    View layoutRecharge;
    @BindView(R.id.tvBtnCharge)
    TextView tvBtnCharge;
    @BindView(R.id.lv_placecate)
    ListView listPlaceCate;

    private List<Fragment> mFragments = new ArrayList<>();
    private Map<String, Integer> mViewPagerHeight = new HashMap<>();

    private MyPagerAdapter mSceneAdapter;
    private SiteDetailsRechargeAdapter mAdapter;
    private PlaceCateTitleAdapter mPlaceadapter;

    private List<AdSiteBean> adbeanlist;

    private SitesBean.Site mData;
    private List<SitePlaceCateBean> mCateList;

    public static void startDetails(Context context, String site_id) {
        Intent intent = new Intent();
        intent.setClass(context, SiteDetailsActivity.class);
        intent.putExtra(Extras.EXTRA_ID, site_id);
        context.startActivity(intent);
}

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_site_details);
    }

    @Override
    protected void initView() {
        super.initView();
        initRecharge();
    }

    @Override
    protected void loadData(boolean loadmore) {
        mAppHelper.apiService().getSiteInfo(getIntent().getStringExtra(Extras.EXTRA_ID)).enqueue(new BaseCallback<ApiResponse<SitesBean.Site>>() {
            @Override
            public void onResponse(Call<ApiResponse<SitesBean.Site>> call, Response<ApiResponse<SitesBean.Site>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful() && response.body().isSccessful()) {
                    mData = response.body().getData();
                    if (null != mData) {
                        initSiteInfo(mData);
                    }
                }

            }

            @Override
            public void onFailure(Call<ApiResponse<SitesBean.Site>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
        mAppHelper.apiService().getSiteOfferList(getIntent().getStringExtra(Extras.EXTRA_ID)).enqueue(new BaseCallback<ApiResponse<List<SiteOfferBean>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<SiteOfferBean>>> call, Response<ApiResponse<List<SiteOfferBean>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful() && response.body().isSccessful()) {
                    List<SiteOfferBean> list = response.body().getData();
                    if (null != list && list.size() > 0) {
                        mAdapter.setItems(list);
                        mAdapter.setCount(list.size() > 2 ? 2 : list.size());
                        if (list.size() <= 2) {
                            layoutRecharge.setVisibility(View.GONE);
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<SiteOfferBean>>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
        mAppHelper.apiService().getSitePlaceCate(getIntent().getStringExtra(Extras.EXTRA_ID)).enqueue(new BaseCallback<ApiResponse<List<SitePlaceCateBean>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<SitePlaceCateBean>>> call, Response<ApiResponse<List<SitePlaceCateBean>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful() && response.body().isSccessful()) {
                    mCateList = response.body().getData();
                    if (null != mCateList && mCateList.size() > 0)
                    {
                        mPlaceadapter=new PlaceCateTitleAdapter(mContext);
                        listPlaceCate.setAdapter(mPlaceadapter);
                        listPlaceCate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                PlaceCateListActivity.startPlaceList(mContext,mCateList.get(i));
                            }
                        });
                        mPlaceadapter.setItems(mCateList);
                    }

                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<SitePlaceCateBean>>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
        mAppHelper.apiService().getSiteAd(getIntent().getStringExtra(Extras.EXTRA_ID)).enqueue(new BaseCallback<ApiResponse<List<AdSiteBean>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<AdSiteBean>>> call, Response<ApiResponse<List<AdSiteBean>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful() && response.body().isSccessful() && response.body().getData().size() > 0) {
                    initSceneViewpager(response.body().getData());
                    adbeanlist=response.body().getData();
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<AdSiteBean>>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
    }

    @OnClick({R.id.btn_call, R.id.btn_recharge_more, R.id.tv_address, R.id.layout_info})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_call:
                SystemUtils.dial(mContext, mData.getStadiumtel());
                break;
            case R.id.btn_recharge_more:
                if (mAdapter.getCount() == 2) {
                    mAdapter.setCount(mAdapter.getItemList().size());
                    imageRechargeArrow.setImageResource(R.mipmap.arrow_down);
                } else {
                    mAdapter.setCount(2);
                    imageRechargeArrow.setImageResource(R.mipmap.arrow_top);
                }
                break;
            case R.id.tv_address:
                MapActivity.startMap(mContext, tvAddress.getText().toString().trim());
                break;
            case R.id.layout_info:
                SiteIntroductionActivity.toSiteIntroduction(mContext, mData);
                break;
        }
    }

    private void initSceneViewpager(List<AdSiteBean> data) {

        List<View> imageViews = new ArrayList<>(); // 滑动的图片集合

        for (final AdSiteBean item : data) {
            final ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            Glide.with(mContext).load(LoadImageUtil.getLoadImageUrl(item.getImages())).placeholder(R.mipmap.ic_launcher).into(imageView);
            imageView.setTag(item);
            imageViews.add(imageView);
           /* imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (item.getLink_url()!=null || !item.getLink_url().equals("") )
                    {
                        WebAcativity.StartWeb(SiteDetailsActivity.this,item.getLink_url());
                    }
                }
            });*/
        }

        CircleNavigator circleNavigator = new CircleNavigator(mContext);
        circleNavigator.setCircleCount(imageViews.size());
        circleNavigator.setCircleColor(Color.WHITE);
        circleNavigator.setCircleClickListener(new CircleNavigator.OnCircleClickListener() {
            @Override
            public void onClick(int index) {
                viewpagerScene.setCurrentItem(index);
            }
        });
        indicatorScene.setNavigator(circleNavigator);
        ViewPagerHelper.bind(indicatorScene, viewpagerScene);
        viewpagerScene.setAdapter(mSceneAdapter = new MyPagerAdapter(imageViews,SiteDetailsActivity.this));
        viewpagerScene.startAutoScroll();



    }

    private void initSiteInfo(SitesBean.Site mData) {
        tvName.setText(mData.getStadiumname());
        tvAddress.setText(mData.getStadiumaddress());
        tvTime.setText("周六日、节假日(" + mData.getStadiumspecialtime() + ")");
        tvTime.append("\n周一至周五(" + mData.getStadiumbusinesstime() + ")");
        ratingLevel.setRating(mData.getStar());
        titleBuilder.setTitleText(mData.getStadiumname());
    }

    private void initRecharge() {
        listRecharge.setAdapter(mAdapter = new SiteDetailsRechargeAdapter(mContext));
        listRecharge.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RechargeOfferActivity.toRechareOffer(mContext, mAdapter.getItem(position).getSid());
            }
        });
        tvBtnCharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RechargeOfferActivity.toRechareOffer(mContext, getIntent().getStringExtra(Extras.EXTRA_ID));
            }
        });
    }


    @Override
    public void onHeightChange(String id, int height) {

    }
}
